package com.xiaouyudeguang.common.utils;

import com.xiaouyudeguang.common.collect.MapBuilder;

import java.util.*;
import java.util.stream.Collectors;

public class MapSort {

    public static <V extends Comparable<V>> Map<String, Object> asc(Map<String, V> map) {
        List<Map.Entry<String, V>> list = map.entrySet()
                .stream()
                .sorted((p1, p2) -> p1.getValue().compareTo(p2.getValue()))
                .collect(Collectors.toList());
        HashMap<String, Object> finalMap = new LinkedHashMap<>();
        list.forEach(ele -> finalMap.put(ele.getKey(), ele.getValue()));
        return finalMap;
    }

    public static <V extends Comparable<V>> Map<String, Object> desc(Map<String, V> map) {
        List<Map.Entry<String, V>> list = map.entrySet()
                .stream()
                .sorted((p2, p1) -> p1.getValue().compareTo(p2.getValue()))
                .collect(Collectors.toList());
        HashMap<String, Object> finalMap = new LinkedHashMap<>();
        list.forEach(ele -> finalMap.put(ele.getKey(), ele.getValue()));
        return finalMap;
    }

    public static void main(String[] args) {
        Map<String, String> map = MapBuilder.builder().put("阿达", "2").put("但是", "1").put("恶女", "3").build();
        System.out.println(desc(map));
        System.out.println(asc(map));
    }

}